package com.liaow.test.retrofit.response;

import org.springframework.http.HttpStatus;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @param <T> 返回数据对象
 * @Description: 通用返回结果封装
 * @Author: L.Wen
 * @Creation_Time: 2020/08/11 22:04
 */
@ApiModel(value = "响应结果")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    // 返回状态
    @ApiModelProperty(value = "状态码")
    private int status;

    // 编码
    @ApiModelProperty(value = "编码定义，详见CodeConstant")
    private String code;

    // 状态描述
    @ApiModelProperty(value = "消息")
    private String desc;

    // 返回数据集
    @ApiModelProperty(value = "数据集")
    private T data;

    public Result(HttpStatus status) {
        this.status = status.value();
        this.desc = status.getReasonPhrase();
    }

    public Result(HttpStatus status, T data) {
        this.status = status.value();
        this.desc = status.getReasonPhrase();
        this.data = data;
    }

    public Result(boolean isSuccess) {
        HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
        if (isSuccess) {
            httpStatus = HttpStatus.OK;
        }
        this.status = httpStatus.value();
        this.desc = httpStatus.getReasonPhrase();
    }

    public Result(HttpStatus status, String code, String desc) {
        this.status = status.value();
        this.code = code;
        this.desc = desc;
    }

    public Result(HttpStatus status, String code, String desc, T data) {
        this.status = status.value();
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

}
